import { _decorator, Component, Animation, tween, Vec3 } from 'cc';
import { CoinDropType } from './Drop';
import { SoundManager } from '../manager/SoundManger';
import EvtManger from '../tools/EvtManger';
import { EventName } from '../manager/AppControll';
const { ccclass, property } = _decorator;

@ccclass('DropCoin')
export class DropCoin extends Component {
    private mEndPoint: Vec3 = null;
    private strOneGold = "oneGold";
    private strTreeGold = "threeGold";
    private strTenGold = "tenGold";
    private mNSpeed = 1000;
    private mType: CoinDropType = null;

    protected onLoad(): void {
        this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.DoMove();
        })
    }

    start() {
    }

    InitDropCoin(endPoint: Vec3, type: CoinDropType) {
        switch (type) {
            case CoinDropType.one:
                this.node.getChildByName("oneGold").active = true;
                this.node.getComponent(Animation).play(this.strOneGold);
                break;
            case CoinDropType.three:
                this.node.getChildByName("threeGold").active = true;
                this.node.getComponent(Animation).play(this.strTreeGold);
                break;
            case CoinDropType.ten:
                this.node.getChildByName("tenGold").active = true;
                this.node.getComponent(Animation).play(this.strTenGold);
                break;
        }
        this.mType = type;
        this.mEndPoint = endPoint;
    }

    DoMove() {
        let node = null;
        switch (this.mType) {
            case CoinDropType.one:
                node = this.node.getChildByName("oneGold");
                break;
            case CoinDropType.three:
                node = this.node.getChildByName("threeGold");
                break;
            case CoinDropType.ten:
                node = this.node.getChildByName("tenGold");
                break;
        }

        let time = this.mEndPoint.clone().subtract(this.node.position).length() / this.mNSpeed;

        const dropAnimation = tween()
            .to(time, { position: this.mEndPoint.clone().subtract(this.node.position) })
            .call(() => {
                SoundManager.Instance<SoundManager>().PlayEffect("ui/getGold");
                EvtManger.do(EventName.coninIntoBag);
            });

        node.children.forEach(nd => {
            tween(nd)
                .then(dropAnimation.clone(nd))
                .start();
            this.scheduleOnce(() => { nd.active = false }
                , time + 0.01);
        })

        this.scheduleOnce(() => {
            this.node.destroy();
        }, time + 0.1)

        // 播放动画

    }
}


